<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
</head>
<body>
<div id="vuebox">
    <h2 style="text-align: center">新闻列表</h2>
    <div class="container" style="margin-top: 50px">
        <div class="row form-inline" role="form" style="padding-left: 250px;">
            <div class="form-group" style="padding-left: 20px;">
                <label>新闻分类:</label>
                <select class="form-control" v-model="categoryId">
                    <option value="0">全部</option>
                    <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                </select>
            </div>
            <div class="form-group" style="padding-left: 20px;">
                <label>新闻标题:</label>
                <input type="text" v-model="title" class="form-control">
            </div>
            <div class="form-group">
                <button type="button" class="btn btn-primary" @click="selectDetail(1)">查询</button>
            </div>
            <div class="text-right">
                <a href="#" class="btn btn-primary" @click.stop="showModal()">新增</a>
            </div>
        </div>
        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>编号</th>
                <th>新闻标题</th>
                <th>新闻摘要</th>
                <th>作者</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in entries.data.list">
                <td>{{item.id}}</td>
                <td>{{item.title}}</td>
                <td>{{item.summary}}</td>
                <td>{{item.author}}</td>
                <td>{{item.cretedate}}</td>
                <td>
                    <button class="btn btn-group" @click="to_update_detail(item.id)">修改</button>
                    <button class="btn btn-success"@click="deleteDetail(item.id)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <br/>
        <div class="text-center">
            <ul class="pagination">
                <li v-show="entries.data.hasPreviousPage">
                    <a href="#" @click.stop="selectDetail(entries.data.perPage)">&laquo;</a>
                </li>
                <li :class="{active:entries.data.pageNum==i}" v-for="i in entries.data.navigatepageNums">
                    <a href="#" @click.stop="selectDetail(i)">{{i}}</a>
                </li>
                <li v-show="entries.data.hasNextPage">
                    <a href="#" @click.stop="selectDetail(entries.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>

        <!--模态框-->
        <div class="modal" id="mymodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" type="button" @click="hideModal()">&times;</button>
                        <div class="modal-title">
                        </div>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="id" class="col-sm-2 control-label">编号</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.id" id="id" type="text" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="categoryid" class="col-sm-2 control-label">新闻分类</label>
                                <div class="col-sm-10">
                                    <select id="categoryid" class="form-control" v-model="new_detail.categoryid">
                                        <option value="0">全部</option>
                                        <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="title" class="col-sm-2 control-label">标题</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.title" id="title" type="text" class="form-control" placeholder="请输入标题">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="summary" class="col-sm-2 control-label">摘要</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.summary" id="summary" type="text" class="form-control" placeholder="请输入摘要"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="author" class="col-sm-2 control-label">作者</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.author" id="author" type="text" class="form-control" placeholder="作者">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="cretedate" class="col-sm-2 control-label">创建时间</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.cretedate" id="cretedate" type="text" class="form-control" placeholder="请输入创建时间">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="updatedate" class="col-sm-2 control-label">修改时间</label>
                                <div class="col-sm-10">
                                    <input v-model="new_detail.updatedate" id="updatedate" type="text" class="form-control" placeholder="请输入修改时间">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-default" @click="hideModal()">关闭</button>
                        <button class="btn btn-primary" @click="saveDetail()">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var vm = new Vue({
        el:"#vuebox",
        data:{
            categoryList:{},
            title:"",
            categoryId:'0',
            entries:{},
            pageNum:'',
            delResult:{},
            new_detail:{id:'',categoryid:'0',title:'',summary:'',author:'',createdate:'',updatedate:''}
        },
        methods:{
            selectDetail:function (pageNum) {
                axios.get("/selectDetail",{params:{title:this.title,categoryId:this.categoryId,pageNum:pageNum}}).then(res=>{
                    this.entries=res.data;
                    this.pageNum=pageNum;
                });
            },
            deleteDetail:function (id) {
                axios.get("/delDetailById",{params:{id:id}}).then(res=>{

                    this.delResult=res.data;
                    if (this.delResult.result=='SUCCESS'){
                        alert("删除成功")
                        this.selectDetail(this.pageNum);
                    }else {
                        alert(this.delResult.message);
                    }
                })
            },
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
                this.clearForm();

            },
            clearForm:function () {
                $('#id').val('');
                $('#categoryid').val(0);
                $('#title').val('');
                $('#summary').val('');
                $('#author').val('');
                $('#createdate').val('');
                $('#updatedate').val('');
            },
            saveDetail:function () {
                axios.post("/saveDetail",this.new_detail).then(res=>{
                    this.delResult=res.data;
                    if (this.delResult.result=='SUCCESS'){
                        this.selectDetail(this.pageNum);
                        this.hideModal();
                        alert("成功！！！")
                    }else {
                        alert(this.delResult.message);
                    }
                })
            },
            to_update_detail:function(id){
                axios.get("/getDetailById",{params:{id:id}}).then(res=>{
                    this.new_detail=res.data.data;
                    this.showModal();
                })
            }
        }
        ,
        mounted(){
            axios.get("/getCategoryList").then(res=>this.categoryList=res.data);
            this.selectDetail(this.pageNum);

        }
    });
</script>